home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue48 / Alfresco / TstCRC.dpr < prev   
Encoding:
Text File  |  1999-06-20  |  5.2 KB  |  177 lines

  1. program TstCRC;
  2.  
  3. {$IFDEF Win32}
  4. {$APPTYPE CONSOLE}
  5. {$ENDIF}
  6.  
  7. uses
  8.   SysUtils,
  9.   {$IFDEF Windows}
  10.   WinCrt,
  11.   {$ENDIF}
  12.   AACRC in 'AACRC.pas';
  13.  
  14.  
  15. var
  16.   i : integer;
  17.   CRC16 : TaaCRC16;
  18.   CRC32 : TaaCRC32;
  19.   CRCTbl16 : Taa16BitCRCTable;
  20.  
  21.   CRC16Calc : TaaCRC16Calculator;
  22.   CRC32Calc : TaaCRC32Calculator;
  23.  
  24.   AAL5Cell : array [0..47] of byte;
  25.  
  26.   St : array [0..47] of char;
  27. begin
  28.   writeln('Testing CRC routines');
  29.   try
  30.     {set up a test message}
  31.     Fillchar(St, sizeof(St), 0);
  32.     StrCopy(St, 'a phrase for which the CRC is wanted');
  33.  
  34.     {calculate the 16-bit CRC with the $1021 polynomial}
  35.     writeln('Standard 16-bit CRC (without extra 16 zero bits)');
  36.     CRC16 := AAGet16BitCRCStd(St, StrLen(St), PolyXMODEMCRC);
  37.     writeln(Format('$%4x', [CRC16]));
  38.     writeln('Standard 16-bit CRC (with extra 16 zero bits)');
  39.     CRC16 := AAGet16BitCRCStd(St, StrLen(St) + 2, PolyXMODEMCRC);
  40.     writeln(Format('$%4x', [CRC16]));
  41.  
  42.     {calculate the 16-bit CRC table}
  43.     AACalc16BitCRCTable(CRCTbl16, PolyXMODEMCRC);
  44.  
  45.     {calculate the 16-bit CRC with this table}
  46.     writeln('Standard 16-bit CRC using table');
  47.     CRC16 := AAGet16BitCRCTbl(St, StrLen(St), CRCTbl16);
  48.     writeln(Format('$%4x', [CRC16]));
  49.  
  50.     readln;
  51.  
  52.     writeln('Testing 16-bit CRC class');
  53.     CRC16Calc := TaaCRC16Calculator.Create(PolyXMODEMCRC, false, 0, false);
  54.     try
  55.       writeln('..optimized bit by bit method');
  56.       CRC16 := CRC16Calc.GetCRCStd(St, StrLen(St));
  57.       writeln(Format('$%4x', [CRC16]));
  58.  
  59.       writeln('..table method');
  60.       CRC16 := CRC16Calc.GetCRC(St, StrLen(St));
  61.       writeln(Format('$%4x', [CRC16]));
  62.  
  63.       writeln('..byte by byte method');
  64.       CRC16 := 0;
  65.       for i := 0 to pred(StrLen(St)) do
  66.         CRC16 := CRC16Calc.UpdateCRC(ord(St[i]), CRC16);
  67.       writeln(Format('$%4x', [CRC16]));
  68.  
  69.       writeln('..saving to include file test16.inc');
  70.       CRC16Calc.SaveToIncFile('test16.inc');
  71.     finally
  72.       CRC16Calc.Free;
  73.     end;
  74.     readln;
  75.  
  76.     writeln('Testing 32-bit CRC class with CRC32 method');
  77.     CRC32Calc := TaaCRC32Calculator.Create(PolyCRC32, true, $FFFFFFFF, true);
  78.     try
  79.       writeln('..optimized bit by bit method');
  80.       CRC32 := CRC32Calc.GetCRCStd(St, StrLen(St));
  81.       writeln(Format('$%8x', [CRC32]));
  82.  
  83.       writeln('..table method');
  84.       CRC32 := CRC32Calc.GetCRC(St, StrLen(St));
  85.       writeln(Format('$%8x', [CRC32]));
  86.  
  87.       writeln('..byte by byte method');
  88.       CRC32 := $FFFFFFFF;
  89.       for i := 0 to pred(StrLen(St)) do
  90.         CRC32 := CRC32Calc.UpdateCRC(ord(St[i]), CRC32);
  91.       CRC32 := not CRC32;
  92.       writeln(Format('$%4x', [CRC32]));
  93.  
  94.       writeln('..saving to include file test32.inc');
  95.       CRC32Calc.SaveToIncFile('test32.inc');
  96.     finally
  97.       CRC32Calc.Free;
  98.     end;
  99.     readln;
  100.  
  101.  
  102.     writeln('Testing 32-bit CRC class with standard AAL5 tests, bitbybit method');
  103.     CRC32Calc := TaaCRC32Calculator.Create(PolyAAL5, false, $FFFFFFFF, true);
  104.     try
  105.       writeln('..all zeros: CRC should be $864d7f99');
  106.       fillchar(AAL5Cell, sizeof(AAL5Cell), 0);
  107.       AAL5Cell[40] := 0;
  108.       AAL5Cell[41] := 0;
  109.       AAL5Cell[42] := 0;
  110.       AAL5Cell[43] := 40;
  111.       CRC32 := CRC32Calc.GetCRCStd(AAL5Cell, 44);
  112.       writeln(Format('$%8x', [CRC32]));
  113.       writeln('..all ones: CRC should be $c55e457a');
  114.       fillchar(AAL5Cell, sizeof(AAL5Cell), $ff);
  115.       AAL5Cell[40] := 0;
  116.       AAL5Cell[41] := 0;
  117.       AAL5Cell[42] := 0;
  118.       AAL5Cell[43] := 40;
  119.       CRC32 := CRC32Calc.GetCRCStd(AAL5Cell, 44);
  120.       writeln(Format('$%8x', [CRC32]));
  121.       writeln('..sequential from 1: CRC should be $bf671ed0');
  122.       for i := 0 to 39 do
  123.         AAL5Cell[i] := succ(i);
  124.       AAL5Cell[40] := 0;
  125.       AAL5Cell[41] := 0;
  126.       AAL5Cell[42] := 0;
  127.       AAL5Cell[43] := 40;
  128.       CRC32 := CRC32Calc.GetCRCStd(AAL5Cell, 44);
  129.       writeln(Format('$%8x', [CRC32]));
  130.     finally
  131.       CRC32Calc.Free;
  132.     end;
  133.     readln;
  134.  
  135.     writeln('Testing 32-bit CRC class with standard AAL5 tests, table method');
  136.     CRC32Calc := TaaCRC32Calculator.Create(PolyAAL5, false, $FFFFFFFF, true);
  137.     try
  138.       writeln('..all zeros: CRC should be $864d7f99');
  139.       fillchar(AAL5Cell, sizeof(AAL5Cell), 0);
  140.       AAL5Cell[40] := 0;
  141.       AAL5Cell[41] := 0;
  142.       AAL5Cell[42] := 0;
  143.       AAL5Cell[43] := 40;
  144.       CRC32 := CRC32Calc.GetCRC(AAL5Cell, 44);
  145.       writeln(Format('$%8x', [CRC32]));
  146.       writeln('..all ones: CRC should be $c55e457a');
  147.       fillchar(AAL5Cell, sizeof(AAL5Cell), $ff);
  148.       AAL5Cell[40] := 0;
  149.       AAL5Cell[41] := 0;
  150.       AAL5Cell[42] := 0;
  151.       AAL5Cell[43] := 40;
  152.       CRC32 := CRC32Calc.GetCRC(AAL5Cell, 44);
  153.       writeln(Format('$%8x', [CRC32]));
  154.       writeln('..sequential from 1: CRC should be $bf671ed0');
  155.       for i := 0 to 39 do
  156.         AAL5Cell[i] := succ(i);
  157.       AAL5Cell[40] := 0;
  158.       AAL5Cell[41] := 0;
  159.       AAL5Cell[42] := 0;
  160.       AAL5Cell[43] := 40;
  161.       CRC32 := CRC32Calc.GetCRC(AAL5Cell, 44);
  162.       writeln(Format('$%8x', [CRC32]));
  163.  
  164.       writeln('..saving to include file test32a.inc');
  165.       CRC32Calc.SaveToIncFile('test32a.inc');
  166.     finally
  167.       CRC32Calc.Free;
  168.     end;
  169.  
  170.   except
  171.     on E:Exception do
  172.       writeln(E.Message);
  173.   end;
  174.   writeln('Done, press Enter to close');
  175.   readln;
  176. end.
  177.